package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.params.FansAdd;
import com.qfedu.provider.entity.DbFuns;
import com.qfedu.provider.entity.DbUser;
import com.qfedu.provider.entity.DbUserDetail;
import com.qfedu.provider.mapper.DbFunsMapper;
import com.qfedu.provider.mapper.DbUserDetailMapper;
import com.qfedu.provider.mapper.DbUserMapper;
import com.qfedu.provider.service.DbFunsService;
import com.qfedu.utils.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【db_funs(粉丝。关注我的)】的数据库操作Service实现
* @createDate 2024-11-23 11:16:39
*/
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class DbFunsServiceImpl extends ServiceImpl<DbFunsMapper, DbFuns>
    implements DbFunsService{
    private final DbUserMapper dbUserMapper;
    private final DbFunsMapper dbFunsMapper;
    private final DbUserDetailMapper dbUserDetailMapper;


    @Override
    public R findByUserId(Integer userId) {
        if(userId == null){
            return R.error("用户id不能为空");
        }
        // 1.校验是否存在该用户
        DbUser dbUser = dbUserMapper.selectById(userId);
        if(dbUser == null){
            return R.error("用户不存在");
        }
        // 2.查询该用户的粉丝
        LambdaQueryWrapper<DbFuns> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbFuns::getUserId, userId);
        wrapper.eq(DbFuns::getStatus, 1);
        List<DbFuns> funsList = dbFunsMapper.selectList(wrapper);
        return R.ok(funsList);
    }

    @Override
    public R saveFans(FansAdd fansAdd) {
        if (fansAdd.getFanId()==fansAdd.getUserId()){
            return R.error("不能关注自己");
        }
        if(fansAdd.getFanId()==null){
            return R.error("用户id不能为空");
        }
        if(fansAdd.getUserId()==null){
            return R.error("博主id不能为空");
        }
        DbUser dbUser = dbUserMapper.selectById(fansAdd.getUserId());
        if(dbUser==null){
            return R.error("被关注人不存在");
        }
        DbUser dbFan = dbUserMapper.selectById(fansAdd.getFanId());
        if (dbFan==null){
            return R.error("关注的人不存在");
        }
        // 查询是否关注过
        LambdaQueryWrapper<DbFuns> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DbFuns::getUserId, fansAdd.getUserId()); // 被关注者
        queryWrapper.eq(DbFuns::getFanId, fansAdd.getFanId()); // 关注着
        DbFuns dbFuns = dbFunsMapper.selectOne(queryWrapper);
        if(dbFuns==null){
            dbFuns = new DbFuns(fansAdd.getUserId(),fansAdd.getFanId(),1);
            if (dbFunsMapper.insert(dbFuns)>0){
                // 被关注的该用户的粉丝量+1
                LambdaQueryWrapper<DbUserDetail> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(DbUserDetail::getUserId, fansAdd.getUserId());
                DbUserDetail userDetail = dbUserDetailMapper.selectOne(wrapper);
                userDetail.setFanNumber(userDetail.getFanNumber()+1);
                dbUserDetailMapper.updateById(userDetail);
                return R.ok(null);

            }
        }else {
            if(dbFuns.getStatus()==0){
                dbFuns.setStatus(1);
                dbFuns.setUpdateTime(new Date());
                if (dbFunsMapper.updateById(dbFuns)>0){
                    // 被关注的该用户的粉丝量+1
                    LambdaQueryWrapper<DbUserDetail> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(DbUserDetail::getUserId, fansAdd.getUserId());
                    DbUserDetail userDetail = dbUserDetailMapper.selectOne(wrapper);
                    userDetail.setFanNumber(userDetail.getFanNumber()+1);
                    dbUserDetailMapper.updateById(userDetail);
                    return R.ok("二次关注");
                }
            }
            return R.error("不能重复关注博主");
        }

        return R.error("系统繁忙，请稍后再试试");
    }

    @Override
    public R lgDelFans(FansAdd fansAdd) {
        if (fansAdd.getFanId()==fansAdd.getUserId()){
            return R.error("不能操作自己");
        }
        if(fansAdd.getFanId()==null){
            return R.error("粉丝id不能为空");
        }
        if(fansAdd.getUserId()==null){
            return R.error("用户id不能为空");
        }
        DbUser dbUser = dbUserMapper.selectById(fansAdd.getUserId());
        if(dbUser==null){
            return R.error("用户不存在");
        }
        DbUser dbFan = dbUserMapper.selectById(fansAdd.getFanId());
        if (dbFan==null){
            return R.error("粉丝不存在");
        }
        //判断是否已经取消关注了
        // 1.查询该用户是否被关注
        LambdaQueryWrapper<DbFuns> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbFuns::getUserId, fansAdd.getUserId());
        wrapper.eq(DbFuns::getFanId, fansAdd.getFanId());
        DbFuns dbFuns = dbFunsMapper.selectOne(wrapper);
        if (dbFuns==null){
            return R.error("该用户没有关注该博主");
        }
        if (dbFuns.getStatus()==0){
            return R.error("该用户已经取消关注该博主");
        }
        dbFuns.setStatus(0);
        // 被关注的该用户的粉丝量-1
        if (dbFunsMapper.updateById(dbFuns)>0){
            // 被关注的该用户的粉丝量+1
            LambdaQueryWrapper<DbUserDetail> detailWrapper = new LambdaQueryWrapper<>();
            detailWrapper.eq(DbUserDetail::getUserId, fansAdd.getUserId());
            DbUserDetail userDetail = dbUserDetailMapper.selectOne(detailWrapper);
            userDetail.setFanNumber(userDetail.getFanNumber()-1);
            dbUserDetailMapper.updateById(userDetail);
            return R.ok(null);
        }

        return R.error("系统繁忙，请稍后再试试");
    }
}




